﻿//-----------------------------------------------------------------------
// <copyright file="Setting.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business
{
    using System;
    using Delta.Data.DataExpress;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// System setting class
    /// </summary>
    [Serializable]
    public class Setting : IDataServicable
    {
        #region Members
        /// <summary>
        /// Setting name
        /// </summary>
        private string name;

        /// <summary>
        /// Setting value
        /// </summary>
        private string value;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Setting class.
        /// </summary>
        public Setting()
            : this(string.Empty, string.Empty)
        {       
        }

        /// <summary>
        /// Initializes a new instance of the Setting class.
        /// </summary>
        /// <param name="name">Setting name</param>
        /// <param name="value">Setting value</param>
        public Setting(string name, string value)
        {
            this.name = name;
            this.value = value;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the setting name
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                if (value.Length < 0)
                {
                    throw new InvalidLengthException("Setting name can't be null.");
                }

                if (value.Length > 50)
                {
                    throw new InvalidLengthException("Setting name can't be greater than 50 characters.");
                }

                this.name = value.ToUpper();
            }
        }

        /// <summary>
        /// Gets or sets the setting value
        /// </summary>
        public string Value
        {
            get
            {
                return this.value;
            }

            set
            {
                if (value.Length > 255)
                {
                    throw new InvalidLengthException("Setting value can't be greater than 255 characters.");
                }

                this.value = value;
            }
        }
        #endregion

        #region Static Load Method
        /// <summary>
        /// Load setting from database
        /// </summary>
        /// <param name="name">Name of the setting</param>
        /// <returns>Filled setting</returns>
        public static Setting Load(string name)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Name", name.ToUpper()));
            Setting setting = DataAccess.GetSingle<Setting>("LoadSetting", System.Data.CommandType.StoredProcedure, parameters);
            if (setting == null)
            {
                setting = new Setting(name.ToUpper(), string.Empty);
            }

            return setting;
        }
        #endregion

        #region Save Method
        /// <summary>
        /// Save setting method
        /// </summary>
        public void Save()
        {
            DataAccess.NonQuery("SaveSetting", System.Data.CommandType.StoredProcedure, GetSaveParameters());
        }
        #endregion

        #region Delete Method
        /// <summary>
        /// Delete setting from database
        /// </summary>
        public void Delete()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Name", this.name));
            DataAccess.NonQuery("DeleteSetting", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Fill Method
        /// <summary>
        /// Fill setting from data source
        /// </summary>
        /// <param name="reader">Data reader</param>
        public void Fill(System.Data.IDataReader reader)
        {
            this.name = Helper.ToString(reader["Name"]);
            this.value = Helper.ToString(reader["Value"]);
        }
        #endregion

        #region Get Save Parameters
        /// <summary>
        /// Get save parameters
        /// </summary>
        /// <returns>Parameter collection</returns>
        public Parameters GetSaveParameters()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Name", this.name));
            parameters.Add(new Parameter("Value", this.value));
            return parameters;
        }
        #endregion
    }
}
